/**
 *  1.CRM考核客户 上做一个trigger
 *  
 *  1.2 新建一条 CRM考核客户评分的数据，考核客户名称=CRM考核客户.客户名，所有人=CRM考核客户.评价人
 *  name=xxxx年y 季度 被评人的姓名 "考核-" 客户名
 *  created by Harry 2017年03月06日
 */
public class GenCRMexamineHandler implements Triggers.handler{
    public void handle(){
        List<CRMCheckCustomer__c> triggernew = (List<CRMCheckCustomer__c>)Trigger.new;
        List<CRMexamine__c> preInsertList = new List<CRMexamine__c>();
        for(CRMCheckCustomer__c crmcc : triggernew){
            CRMexamine__c exam = new CRMexamine__c();
            exam.name=crmcc.Year01__c+'年'+crmcc.Q__c+crmcc.BeingJudgedName__c+'考核-'+crmcc.CustomerName__c;
            exam.OwnerId=crmcc.BeingJudged__c;
            exam.examinename__c=crmcc.Customer__c;
            exam.CRMCheckCustomer__c=crmcc.Id;
            preInsertList.add(exam);
        }
        insert preInsertList;
    }
    public static  void updateCRMexamine(List<CRMCheckCustomer__c> list_new){
    	Map<Id,CRMCheckCustomer__c> cMap = new Map<Id,CRMCheckCustomer__c>();
        for(CRMCheckCustomer__c c :list_new){
            cMap.put(c.Id,c);
            c.OwnerId = c.BeingJudged__c;
        }
       if(cMap.keySet().size() > 0){
           List<CRMexamine__c> ceList = new List<CRMexamine__c>();
       		for(CRMexamine__c ce:[select
                                  CRMCheckCustomer__c,examinename__c
                                  from CRMexamine__c where CRMCheckCustomer__c in:cMap.keySet()]){
                
                if(cMap.containsKey(ce.CRMCheckCustomer__c)){
                    CRMCheckCustomer__c c = cMap.get(ce.CRMCheckCustomer__c);
                	ce.examinename__c = c.Customer__c;
                    ce.name=c.Year01__c+'年'+c.Q__c+c.BeingJudgedName__c+'考核-'+c.CustomerName__c;
				  	ce.OwnerId = c.BeingJudged__c;
                    ceList.add(ce);
                }
       		  
       		}
           if(ceList.size()>0) update ceList;
       }
       
    }
    
}